package com.wastrel.handwritedemo;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import butterknife.Bind;
import butterknife.ButterKnife;

import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.btn1)
    Button btn1;
    @Bind(R.id.btn2)
    Button btn2;
    @Bind(R.id.img1)
    ImageView img1;
    @Bind(R.id.img2)
    ImageView img2;
    public static String path= Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qm.png";
    public static String path1= Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ls.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, HandWriteActivity.class), 1);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, LandscapeActivity.class), 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==100)
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
                         options.inSampleSize = 2;
                          Bitmap bm = BitmapFactory.decodeFile(path, options);


      Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.test);
            BitmapDrawable  bd = (BitmapDrawable) drawable;
            Bitmap bm2 =bd.getBitmap();

            Bitmap bm3 = Bitmap.createBitmap(bm2.getWidth(),bm2.getHeight(),bm2.getConfig());

            Canvas canvas = new Canvas(bm3);

            canvas.drawBitmap(bm2,new Matrix(),null);
            canvas.drawBitmap(bm,(int)(bm2.getWidth()/3),bm2.getHeight()-bm.getHeight(),null);
            //将合并后的bitmap 保存为png 图片到本地
            String a ="/sdcard/qm1.png";
            save2(a,bm3);

            img1.setImageBitmap(bm3);

        }else if(resultCode==101)

        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 3;
            Bitmap bm = BitmapFactory.decodeFile(path1, options);


            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.test);
            BitmapDrawable  bd = (BitmapDrawable) drawable;
            Bitmap bm2 =bd.getBitmap();

            Bitmap bm3 = Bitmap.createBitmap(bm2.getWidth(),bm2.getHeight(),bm2.getConfig());

            Canvas canvas = new Canvas(bm3);

            canvas.drawBitmap(bm2,new Matrix(),null);
            canvas.drawBitmap(bm,(int)(bm2.getWidth()/4),bm2.getHeight()-bm.getHeight(),null);
            //将合并后的bitmap 保存为png 图片到本地
            String a ="/sdcard/qm1.png";
            save2(a,bm3);

            img1.setImageBitmap(bm3);

           // img2.setImageBitmap(bm);
        }

    }


    public void save(String path) {


        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = bos.toByteArray();
        if (buffer != null) {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);

                outputStream.write(buffer);
                outputStream.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

            //保存图片后发送广播通知更新数据库
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            sendBroadcast(intent);//这个广播的目的就是更新图库，发了这个广播进入相册就可以找到你保存的图片了！，记得要传你更新的file哦

        }
    }

    public void save2(String path, Bitmap bit ) {

        Bitmap bitmap = bit;
        //BitmapUtil.createScaledBitmapByHeight(srcBitmap, 300);//  压缩图片

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
        byte[] buffer = bos.toByteArray();
        if (buffer != null) {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                outputStream.write(buffer);
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }


            //保存图片后发送广播通知更新数据库
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            sendBroadcast(intent);//这个广播的目的就是更新图库，发了这个广播进入相册就可以找到你保存的图片了！，记得要传你更新的file哦

        }
    }
}
